package com.nulldev.util.java.swing.extSwing;

import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import com.nulldev.util.java.swing.SwingUtil;
import com.nulldev.util.java.swing.extSwing.lookandfeel.themes.Theme;
import com.nulldev.util.java.swing.extSwing.lookandfeel.themes.ThemeApplier;
import com.nulldev.util.math.MathUtil;
import com.nulldev.util.threading.legacy.ThreadExecutor;

import javax.swing.JProgressBar;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;

public class eActiveProgressBar extends JFrame {

	private JPanel contentPane;
	private JProgressBar progressBar;

	private boolean indter;
	private int x = 0;
	private boolean countUP = true;
	private boolean haltF;
	private final Runnable r = new Runnable() {

		@Override
		public void run() {
			while (true) {
				if (haltF) {
					break;
				}
				try {
					if (indter) {
						if (x >= progressBar.getMaximum()) {
							countUP = false;
						} else if (x <= progressBar.getMinimum()) {
							countUP = true;
						}
						if (countUP) {
							x += 1;
							progressBar.setValue(x);
						} else {
							x -= 1;
							progressBar.setValue(x);
						}
					}
					Thread.sleep(SwingUtil.FPS_60);
				} catch (Exception e) {
				}
			}
		}
	};
	private JLabel lblLoading;

	/**
	 * Launch the application.
	 */
	public static eActiveProgressBar main() {
		final eActiveProgressBar frame = new eActiveProgressBar();
		frame.setVisible(true);
		ThemeApplier.applyTheme(frame);
		frame.setLocation(
				(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode().getWidth()
						- frame.getWidth()) / 2,
				(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode().getHeight()
						- frame.getHeight()) / 2);
		return frame;
	}

	private JProgressBar getBar() {
		return this.progressBar;
	}

	public void setMin(final int min) {
		this.progressBar.setMinimum(this.x = min);
	}

	public void setMax(final int max) {
		if (max > this.progressBar.getMinimum()) {
			this.progressBar.setMaximum(max);
		}
	}

	public void setStatus(final int value) {
		if (this.indter) {
			return;
		}
		this.progressBar.setValue(MathUtil.clamp(this.progressBar.getMinimum(), this.progressBar.getMaximum(), value));
	}

	/**
	 * Create the frame.
	 */
	public eActiveProgressBar() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(eActiveConsoledBar.class.getResource("/com/nulldev/util/java/swing/nullutil_logo.png")));
		setTitle("extSwing.eActiveProgressBar");
		setResizable(false);
		if (ThemeApplier.useSystem) {
			try {
				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			} catch (Exception e) {
			}
		}

		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 365, 124);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		progressBar = new JProgressBar();

		lblLoading = new JLabel("Loading...");
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane
				.setHorizontalGroup(
						gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(
										gl_contentPane.createSequentialGroup().addContainerGap()
												.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
														.addComponent(progressBar, GroupLayout.DEFAULT_SIZE, 331,
																Short.MAX_VALUE)
														.addComponent(lblLoading))
												.addContainerGap()));
		gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup().addGap(32)
						.addComponent(progressBar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED).addComponent(lblLoading)
						.addContainerGap(19, Short.MAX_VALUE)));
		contentPane.setLayout(gl_contentPane);
		ThreadExecutor.execute(this.r);
	}

	public void setIndeterminate(final boolean state) {
		this.indter = state;
	}

	public JLabel getLabel() {
		return lblLoading;
	}

	public void end() {
		this.haltF = true;
		this.setVisible(false);
		this.dispose();
	}

	public void setText(final String string) {
		this.getLabel().setText(string);
	}
}
